package com.haiyangwang1.lesson12.bookSystem;

import java.util.Arrays;
import java.util.Scanner;

public class BookManager {
    Book[] books = new Book[50];
    //记录图书数量（记录图书最后位置一个到哪里）,记录到最后一本图书的后面一位，也就是index位置的数据为null
    int index=0;

    Scanner input = new Scanner(System.in);

    public BookManager() {
        books[0] = new Book(1, "斗破苍穹", 1, 100, null);
        books[1] = new Book(2, "斗罗大陆", 0, 90, "2022-01-11");
        books[2] = new Book(3, "甜心大少", 0, 99, "2022-02-23");
        books[3] = new Book(4, "霸道总裁", 1, 80, null);
        books[4] = new Book(5, "都市公子", 0, 81, "2022-04-01");
        index+=5;
    }

    public void menu() {
        String answer = "y";
        while (answer.equalsIgnoreCase("y")) {
            System.out.println("欢迎使用图书管理系统");
            System.out.println("--------------------");
            System.out.println("1.查看图书");
            System.out.println("2.新增图书");
            System.out.println("3.删除图书");
            System.out.println("4.借出图书");
            System.out.println("5.归还图书");
            System.out.println("6.排行榜");
            System.out.println("0.返回主菜单");
            System.out.println("--------------------");
            System.out.println("请选择0-6:");
            int choose = input.nextInt();
            switch (choose) {
                case 1:
                    list();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    delete();
                    break;
                case 4:
                    lend();
                    break;
                case 5:
                    returnBook();
                    break;
                case 6:
                    rank();
                    break;
            }
            System.out.println("请问你还要继续吗？(y/n)");
            answer = input.next();

        }

    }

    //排行榜
    public void rank(){
        //创建一个新的数组，用于显示排行信息
        Book[] bookRank=new Book[index];
        //备份原来的数据到新的排行榜数据中
        for (int i = 0; i < bookRank.length; i++) {
            bookRank[i]=books[i];
        }
        Book temp;
        for (int i=0;i< bookRank.length;i++){
            for (int j = i; j < bookRank.length; j++) {
                if(bookRank[i].count<bookRank[j].count){
                    temp=bookRank[i];
                    bookRank[i]=bookRank[j];
                    bookRank[j]=temp;
                }
            }
        }
        for (int i = 0; i < index; i++) {
            if (bookRank[i] != null) {
                System.out.println(bookRank[i].id + "\t书名为：" + bookRank[i].name + "\t状态" + (bookRank[i].status == 1 ? "没有借出" : "已借出")
                        + "\t借出数量" + bookRank[i].count + "\t\t\t\t借出时间" + bookRank[i].time);
            }
        }

    }
    //还书
    public void returnBook(){

    }

    //借书
    public void lend(){
        //书没有被借出
    }

    //删除图书
    public void delete() {
//        boolean flag = false;
//        System.out.println("请输入你要删除的书的序号");
//        int del = input.nextInt();
//        for (int i = 0; i < books.length; i++) {
//            if (books[i] != null) {
//                if (books[i].id == del && books[i].status == 1) {
//                    int j = i;
//                    while (books[j + 1] != null) {
//                        books[j].id = books[j + 1].id;
//                        books[j].name = books[j + 1].name;
//                        books[j].status = books[j + 1].status;
//                        books[j].count = books[j + 1].count;
//                        books[j].time = books[j + 1].time;
//                        j++;
//                    }
//                    boolean b = books[j] == null;
//                    flag=true;
//                    } else if (books[i].name.equals(del)&&books[i].status==0) {
//                    flag=false;
//                }
//            }
//            }
//        if (flag){
//            System.out.println("删除成功");
//        }else {
//            System.out.println("删除失败");
//        }

        System.out.print("请输入你要删除的书的序号");
        int del = input.nextInt()-1;//之所以减1，是因为数组索引是从0开始，但是用户看到的是从1开始的
        while(del<0&&del>=index){
            System.out.print("现在数据有"+index+"本，输入错误，请重新输入你要删除的书的序号");
            del = input.nextInt();
        }
        if(del>0&&del<index){
            for (int i = del; i <index ; i++) {
                books[i]=books[i+1];
            }
        }
 }





    //添加图书
    public void add() {
//        int index = -1;
//        for (int i = 0; i < books.length; i++) {
//            if (books[i] == null) {
//                index = i;
//                break;
//            }
//        }

        if (index != -1) {
            books[index] = new Book();
            System.out.println("请输入新增图书的id:");
            books[index].setId( input.nextInt());
            System.out.println("请输入新增图书的名字：");
            books[index].setName(input.next());
            books[index].status = 1;
            books[index].count = 0;
            books[index].time = null;
            System.out.println("添加图书成功");
        } else {
            System.out.println("图书馆的书已经藏满啦");
        }

    }

    //查看图书
    public void list() {
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
//            books[0]=new Book();
                System.out.println(books[i].id + "\t书名为：" + books[i].name + "\t状态" + (books[i].status == 1 ? "没有借出" : "已借出") + "\t借出数量" + books[i].count + "\t\t\t\t借出时间" + books[i].time);
            }
        }
    }

}
